import { Entity, Column, ManyToMany, Index } from 'typeorm';
import { BaseEntity } from '@app/common/entities/base.entity';
import { User } from './user.entity';

@Entity('roles')
@Index(['name'])
export class Role extends BaseEntity {
    @Column({ type: 'varchar', length: 50 })
    name: string;

    @Column({ type: 'varchar', length: 100 })
    label: string;

    @Column({ type: 'varchar', length: 255, nullable: true })
    description?: string;

    @Column({ type: 'tinyint', default: 1, comment: '状态: 0=禁用, 1=启用' })
    status: number;

    @Column({ type: 'int', default: 0 })
    sort: number;

    @Column({ type: 'json', nullable: true })
    permissions?: string[];

    // 角色用户关联
    @ManyToMany(() => User, user => user.roles)
    users: User[];

    // 虚拟属性：获取用户数量
    get userCount(): number {
        return this.users?.length || 0;
    }

    // 虚拟属性：检查是否为系统角色
    get isSystem(): boolean {
        return ['admin', 'editor', 'author', 'user'].includes(this.name);
    }
}
